package com.example.xyxwebbackend001.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.xyxwebbackend001.mapper.ArticleMapper;
import com.example.xyxwebbackend001.mapper.CPOSTSMapper;
import com.example.xyxwebbackend001.mapper.HomeImagesDataMapper;
import com.example.xyxwebbackend001.model.DTO.ArticleSimpleDTO;
import com.example.xyxwebbackend001.model.entity.HomeImagesData;
import com.example.xyxwebbackend001.service.FileStorageService;
import com.example.xyxwebbackend001.service.HomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class HomeServiceImpl implements HomeService {

    @Autowired
    private HomeImagesDataMapper homeImagesDataMapper;

    @Autowired
    private FileStorageService fileStorageService;

    @Autowired
    private ArticleMapper articleMapper;
    @Override
    public List<HomeImagesData> getCarouselList(String title) {
        QueryWrapper<HomeImagesData> wrapper = new QueryWrapper<>();
        wrapper.ne("status", 2);
        if (StringUtils.hasText(title)) {
            wrapper.like("title", title);
        }
        return homeImagesDataMapper.selectList(wrapper);
    }

    public void updateCarouselStatus(Integer id, Integer status) {
        UpdateWrapper<HomeImagesData> wrapper = new UpdateWrapper<>();
        wrapper.set("status", status)
                .eq("home_image_id", id);
        homeImagesDataMapper.update(null, wrapper);
    }

    @Override
    public void createCarousel(HomeImagesData data) {
        homeImagesDataMapper.insert(data);
    }

    @Override
    public List<ArticleSimpleDTO> getArticleListByViews(String sort) {
        List<ArticleSimpleDTO> articleList = articleMapper.getSortedArticles(sort);
        articleList.forEach(ArticleSimpleDTO::convertTagToName);
        return articleList;
    }
}
